<template>
  <div class="app-container bpm-btn">
    <Card dis-hover class="task-card">

      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="生效时间" prop="startDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="queryParams.startDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择开始生效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效时间 " prop="endDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="queryParams.endDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择失效时间 ">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="委托说明" prop="opinion">
          <el-input
            v-model="queryParams.opinion"
            placeholder="请输入委托说明"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="settingList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column
          label="序号"
          type="index"
          width="50"
          fixed
          align="center">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum-1 ) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="标题" align="center" prop="subject"/>
        <el-table-column label="开始生效时间" align="center" prop="startDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="失效时间 " align="center" prop="endDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间    " align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="代理范围" align="center" prop="agentType">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.agentType==1" type="success">全部流程</el-tag>
            <el-tag v-else type="danger">部分流程</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="委托说明" align="center" prop="opinion"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改流程代理设置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules" label-width="128px">
          <el-form-item label="被委托人" prop="authId">
            <Button
              v-for="(item,index) in form.authIds"
              v-model="form.authIds"
              type="primary"
              style="margin-left: 2px"
              color="primary"
              @click="delCommonUsers(index)"
            >
              {{ item.fullname }} &nbsp;<Icon type="md-close"/>

            </Button>

            <Button type="error" @click="selectInformedUsers">
              <Icon type="ios-contact"/>
            </Button>
          </el-form-item>
          <el-form-item label="标题" prop="subject">
            <el-input v-model="form.subject" placeholder="请输入标题"/>
          </el-form-item>
          <el-form-item label="开始生效时间" prop="startDate">
            <el-date-picker clearable size="small" style="width: 200px"
                            v-model="form.startDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择开始生效时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="失效时间 " prop="endDate">
            <el-date-picker clearable size="small" style="width: 200px"
                            v-model="form.endDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择失效时间 ">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="代理范围" prop="agentType">
            <el-select v-model="form.agentType" placeholder="请选择代理范围 1,全部流程,2,部分流程,3.流程节点">
              <el-option v-for="item in dictItem" :value="item.itemCode" :label="item.itemName"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="授权流程" prop="process" v-if="form.agentType=='2'">
            <Button
              v-for="(item,index) in form.process"
              v-model="form.process"
              type="primary"
              style="margin-left: 2px"
              color="primary"
              @click="deleteProcess(index)"
            >
              {{ item.name }} &nbsp;<Icon type="md-close"/>

            </Button>

            <Button type="error" @click="selectProcess">
              <Icon type="ios-add-circle-outline" />
            </Button>
          </el-form-item>
          <el-form-item label="委托说明" prop="opinion">
            <el-input type="textarea" v-model="form.opinion" placeholder="请输入委托说明"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
        <user-multiple-dialog
          :on-ok="confirmDialogEvent"
          :show.sync="show"
          :exist-data="existData"
        />
        <process-user-multiple-dialog
          :single-show.sync="singleShow"
          :exist-data="form.process"
          :on-ok="confirmProcessDialogEvent"
          >

        </process-user-multiple-dialog>

      </el-dialog>
    </Card>


  </div>
</template>

<script>
  import {addSetting, delSetting, exportSetting, getSetting, listSetting, updateSetting} from "@/api/setting/setting";
  import {getItemByCode} from "@/api/setting";
  import userMultipleDialog from '@/views/components/org/userMultipleDialog'
  import processUserMultipleDialog from '@/views/components/org/processUserMultipleDialog'

  export default {
    name: "Setting",
    components: {
      userMultipleDialog,
      processUserMultipleDialog

    },
    data() {
      return {
        dictItem: [],
        existData: [],
        show: false,
        singleShow: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 流程代理设置表格数据
        settingList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          subject: null,
          authId: null,
          startDate: null,
          endDate: null,
          createBy: null,
          createTime: null,
          agentType: null,
          updateBy: null,
          updateTime: null,
          tenantId: null,
          opinion: null
        },
        // 表单参数
        form: {
          id: null,
          subject: null,
          authId: null,
          startDate: null,
          endDate: null,
          createBy: null,
          createTime: null,
          agentType: '1',
          updateBy: null,
          updateTime: null,
          tenantId: null,
          opinion: null,
          authIds:[],
          process:[],

        },
        // 表单校验
        rules: {
          startDate: [
            {required: true, message: "开始生效时间不能为空", trigger: "blur"}
          ],
          endDate: [
            {required: true, message: "失效时间 不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建时间    不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "更新时间不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.initDict()
    },
    methods: {
      delCommonUsers: function (index) {
        this.form.authIds.splice(index, 1)
        this.existData.splice(index, 1)
      },
      deleteProcess: function (index) {
        this.form.process.splice(index, 1)
      },
      selectInformedUsers: function () {
        try {
          this.existData =  this.form.authIds;

        } catch (e) {
          this.existData = []
        }
        this.show = true
      },
      selectProcess: function () {
        // try {
        //   this.existData =  this.form.authIds;
        //
        // } catch (e) {
        //   this.existData = []
        // }
        this.singleShow = true
      },
      confirmDialogEvent: function (data) {
        if (data) {
          this.form.authIds=data
          this.existData=data
        }
      },
      confirmProcessDialogEvent: function (data) {
        if (data) {
          this.form.process=data
        }
      },

      initDict() {
        getItemByCode("setting_type").then(response => {
          if (response.code == 0) {
            this.dictItem = response.data
          }
        });
      },
      /** 查询流程代理设置列表 */
      getList() {
        this.loading = true;
        listSetting(this.queryParams).then(response => {
          this.settingList = response.data;
          this.total = response.count;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          subject: null,
          authId: null,
          startDate: null,
          endDate: null,
          createBy: null,
          createTime: null,
          agentType: '1',
          updateBy: null,
          updateTime: null,
          tenantId: null,
          opinion: null,
          authIds:[],
          process:[]
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加流程代理设置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.initDict();
        this.reset();
        const id = row.id || this.ids
        getSetting(id).then(response => {
          this.form = response.data;
          this.form.agentType= response.data.agentType+''
          this.open = true;
          this.title = "修改流程代理设置";
        });

      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSetting(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSetting(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除流程代理设置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delSetting(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有流程代理设置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportSetting(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style>
  .bpm-btn .el-form-item__label {
    padding-left: 0 !important;
  }
</style>
<style lang="scss" scoped>
  /* button color */
  .el-button--cyan.is-active,
  .el-button--cyan:active {
    background: #20B2AA;
    border-color: #20B2AA;
    color: #FFFFFF;
  }

  .el-button--cyan:focus,
  .el-button--cyan:hover {
    background: #48D1CC;
    border-color: #48D1CC;
    color: #FFFFFF;
  }

  .el-button--cyan {
    background-color: #20B2AA;
    border-color: #20B2AA;
    color: #FFFFFF;
  }

  /* text color */
  .text-navy {
    color: #1ab394;
  }

  .text-primary {
    color: inherit;
  }

  .text-success {
    color: #1c84c6;
  }

  .text-info {
    color: #23c6c8;
  }

  .text-warning {
    color: #f8ac59;
  }

  .text-danger {
    color: #ed5565;
  }

  .text-muted {
    color: #888888;
  }

  /* image */
  .img-circle {
    border-radius: 50%;
  }

  .img-lg {
    width: 120px;
    height: 120px;
  }

  .avatar-upload-preview {
    position: absolute;
    top: 50%;
    transform: translate(50%, -50%);
    width: 200px;
    height: 200px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;
    overflow: hidden;
  }

  /* 拖拽列样式 */
  .sortable-ghost {
    opacity: .8;
    color: #fff !important;
    background: #42b983 !important;
  }

  .top-right-btn {
    position: relative;
    float: right;
  }

  .main-div {
    background-color: white !important;
  }

</style>
